<?php
if ( !defined( "IN_OP" ) )
{
    exit( "You can't access this file directly..." );
}
include_once( "member/proc_betin.php" );
$ui = new UI();
$tpl = new Template2();
$tpl->load( array( "news" => "news.htm" ) );
$conf = new Config();
$news = $conf->Get( "news_th" );
if ( $news == "" )
{
    $news = $conf->Get( "news" );
}
else
{
    $news = "<font color=red>".$news."</font>&#12288;".$conf->Get( "news" );
}
$tpl->set( "newsmessage", $news );
$tpl->show( "news" );
$tpl->set( "account", $curruser['account'] );
if ( $curruser['enabled'] == 0 || $curruser['enabled'] == 2 )
{
    $ui->showErrorPage( "<p class=error>帐号已被暂停使用</p>" );
    exit();
}
$rs = getdb( "select * from lotto where open>='".date( "Y-m-d" )."' order by open" );
if ( $rs->eof )
{
    $ui->showErrorPage( "<p class=error>目前没有可以下注期数！</p>" );
    exit();
}
$open = $rs->f['open'];
$round = $rs->f['round'];
$lotto_id = $rs->f['id'];
$othebet = $conf->get( "othebet" );
$stopbet = $conf->get( "stopbet" );
$waitbet = $conf->get( "waitbet" );
if ( date( "Y-m-d" ) < $open )
{
    exit();
}
else if ( date( "Y-m-d" ) == $open )
{
    if ( $stopbet <= date( "Hi" ) )
    {
        $ui->showErrorPage( "<p>下注期数：".d3( $round )."<br>开奖日：".$open."</p>"."<p class=error>已经截止下注！</p>" );
        exit();
    }
    else if ( date( "Hi" ) <= $conf->get( "startbet" ) )
    {
        exit();
    }
}
$t1 = strtotime( $open." ".$stopbet );
$tpl->Set( "timestap", $t1 - systime( ) );
$tpl->set( "open", $open );
$tpl->set( "round", d3( $round ) );
$tpl->set( "lotto_id", $lotto_id );
$tpl->set( "account", $curruser['account'] );
$tpl->set( "amount", $curruser['amount'] );
$disk = $g_disk_code[getUserValidDisk( $curruser )];
$rates = array();
$rs0 = getdb( "select * from rate0".$disk." where lotto_id={$lotto_id}" );
if ( $rs0->eof )
{
    $ui->showErrorPage( "<p class=error>系统目前维护中，请稍後！</p>" );
    exit();
}
$rates += $rs0->f;
$rs1 = getdb( "select * from rate1".$disk." where lotto_id={$lotto_id}" );
if ( $rs1->eof )
{
    $ui->showErrorPage( "<p class=error>系统目前维护中，请稍後！</p>" );
    exit();
}
$rates += $rs1->f;
$rs5 = getdb( "select * from rate5".$disk." where lotto_id={$lotto_id}" );
if ( $rs5->eof )
{
    $ui->showErrorPage( "<p class=error>系统目前维护中，请稍後！</p>" );
    exit();
}
$rates += $rs5->f;
$betmin = $conf->get( "betmin" );
$tpl->set( "min", $betmin );
$betmax = $conf->get( "betmax" );
$tpl->set( "max", $betmax );
$tpl->set( "max_str", $betmax );
$rs = getdb( "select amount_6h from k_user_bet where user_id=".$curruser['id']." and lotto_id=".$lotto_id );
$usedamount = $rs->f[0] ? $rs->f[0] : 0;
$rs->close();
$limit = array();
$limit[0] = GetAmountInfo( 0 );
$ok = isset( $_POST['ok'] );
if ( $ok )
{
    echo "<body>";
}
else
{
    echo "<body onload='StartFocus()'>";
}
echo "<form action=\"op.php?op=member&fp=proc0a\" method=post onsubmit=\"this.submit.disabled=true;this.reset.disabled=true\">";
echo "<input type=hidden name=ok value=1>";
echo "<input type=hidden name=disk value=".strpos( " abcd", $disk ).">";
$waittime = 2000;
$betcount = 0;
$total = 0;
$tmp = "";
$oddname = array( "", "单", "双" );
$bigname = array( "", "大", "小" );
$chinayearname = array( "", "家禽", "野兽" );
$colorname = array( "", "红波", "蓝波", "绿波" );
$usemaxamount = $curruser['usemaxamount'];
$user_rate0 = get_share_rate0( $curruser['id'] );
$rate0_8num = get_share_rate0_8( $curruser['id'] );
$user_rate8 = $user_rate0 * $rate0_8num;
$u2 = get_user( $curruser['prev_id'] );
$u3 = get_user( $u2['prev_id'] );
$u4 = get_user( $u3['prev_id'] );
$u5 = get_user( $u4['prev_id'] );
if ( isset( $_POST['r0'] ) )
{
    $money = intval( $_POST['r0m'] );
    if ( 0 < $money )
    {
        if ( !$ok )
        {
            echo "<input type=hidden name=r0m value=".$money.">";
        }
        foreach ( $_POST['r0'] as $val )
        {
            ++$betcount;
            $err = 0;
            $tmp .= "<tr class=td2>";
            $tmp .= "<td>".$g_betname[0]."</td>";
            $tmp .= "<td>".$val."</td>";
            $sub_rates = get_subs_rates( $u5['id'], $lotto_id, 0, $val );
            $rate = $rates["r".$val] - $sub_rates - $user_rate0;
            $tmp .= "<td align=right>".trimfloat( $rate, 1 )."</td>";
            $tmp .= "<td align=right>".$money."</td>";
            if ( $ok )
            {
                $tmp .= "<td>";
                $betallow = $limit[0][2];
                $oneamount = $limit[0][1];
                $maxamount = get_bet0_money( 0, $val );
                if ( 0 < $maxamount )
                {
                    $oneamount = min( $oneamount, $maxamount );
                }
                if ( $money < $betmin )
                {
                    $tmp .= "<font color=red>小於最小限额";
                    $waittime += 200;
                }
                else if ( $curruser['balance'] < $money + $usedamount )
                {
                    $tmp .= "<font color=red>超过信用额度";
                    $waittime += 200;
                }
                else if ( $usemaxamount && $curruser['maxamount'] < $money + $usedamount )
                {
                    $tmp .= "<font color=red>超过本期限额";
                    $waittime += 200;
                }
                else if ( $oneamount < $money )
                {
                    $tmp .= "<font color=red>超过单注限额";
                    $waittime += 200;
                }
                else if ( $rate <= 0 || 55 < $rate )
                {
                    $tmp .= "<font color=red>赔率数值错误";
                    $waittime += 200;
                }
                else
                {
                    $n = getnumberamount( $curruser['id'], $lotto_id, 0, $val );
                    if ( $limit[0][2] < $money + $n )
                    {
                        $err = 1;
                    }
                    if ( $conf->get( "stopbet" ) <= date( "Hi" ) && $err == 0 )
                    {
                        $err = 3;
                    }
                    if ( $err == 1 )
                    {
                        $tmp .= "<font color=red>超过单项限额";
                        $waittime += 200;
                    }
                    else if ( $err == 2 )
                    {
                        $tmp .= "<font color=red>超过下注总额";
                        $waittime += 200;
                    }
                    else if ( $err == 3 )
                    {
                        $tmp .= "<font color=red><b>已经截止下注</b>";
                        $waittime += 200;
                    }
                    else
                    {
                        $stime = systime();
                        $p = array( 0, $val );
                        if ( betin( $p, $money, $rate, $sub_rates, $u2, $u3, $u4, $u5, $stime, $disk ) )
                        {
                            $tmp .= "<font color=green>成功 ";
                            $usedamount += $money;
                            $total += $money;
                        }
                        else
                        {
                            $tmp .= "<font color=red>超过信用额度";
                            break;
                        }
                    }
                }
                $tmp .= "</font></td>";
            }
            else
            {
                echo "<input type=hidden name=\"r0[]\" value=\"".$val."\">";
                $total += $money;
            }
            $tmp .= "</tr>";
        }
    }
}
if ( $betcount == 0 )
{
    echo "<p><font color=red><b>请输入下注金额！</b></font></p>";
    echo "<p><input type=button value='回上一页' onclick='history.back()'></p>";
    exit();
}
echo "您下注的是：".strtoupper( $disk )." 盘";
echo "<table border=0 cellpadding=2 cellspacing=1 class=table0>";
echo "<tr class=td0><td>下注方式</td><td>下注内容</td><td>赔率</td><td>下注金额</td>";
if ( $ok )
{
    echo "<td>结果</td>";
}
echo "</tr>";
echo $tmp;
echo "<tr class=td0 align=right><td colspan=3>合计</td><td>".$total."</td>".( $ok ? "<td>&nbsp;</td>" : "" )."</tr>";
echo "</table>";
if ( $ok )
{
    echo "<script>";
    echo "parent.leftFrame.location.href='op.php?op=member&fp=account';";
    echo "setTimeout(\"location.href='op.php?op=member&fp=bet0a'\",".$waittime.");";
    echo "</script>";
}
else
{
    echo "<br>";
    echo "<input type=submit name=submit value=确定下注>&#12288;<input name=reset type=reset value=取消下注 onclick='history.back()'>";
    echo "<Script>function StartFocus(){  document.forms[0].submit.focus();}</Script>";
    echo "</form>";
}
?>